class Solution(object):
    def maxBoxesInWarehouse(self, boxes, warehouse):
        n = len(warehouse)
        left = warehouse.copy()
        right = warehouse.copy()
        for i in range(1, n):
            left[i] = min(left[i - 1], left[i])
            right[n - 1 - i] = min(right[n - 1 - i], right[n - i])
        for i in range(n):
            left[i] = max(left[i], right[i])
        left.sort()
        boxes.sort()
        m = len(boxes)
        l_index = 0
        r_idnex = 0
        ans = 0
        while l_index < m and r_idnex < n:
            if left[r_idnex] >= boxes[l_index]:
                ans += 1
                l_index += 1
                r_idnex += 1
            else:
                r_idnex += 1
        return ans

data = Solution()
boxes = [1,2,2,3,4]
warehouse = [3,4,1,2]
print(data.maxBoxesInWarehouse(boxes, warehouse))